//
//  NetworkTool.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/21.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import AFNetworking

private let errorDomain = "com.baidu.data.error"

enum HTTPMethod:String {
    case POST = "POST"
    case GET = "GET"
}

class NetworkTool : AFHTTPSessionManager {
    
    //swift单例的定义方法
    static let sharedNetworkTool : NetworkTool = {
        let baseurl = NSURL(string: "https://api.weibo.com/")
       let tools = NetworkTool(baseURL: baseurl)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    func requestJSONDict(httpmethod : HTTPMethod,parameters : [String : String],urlString : String, finish:(dict : [String : AnyObject]?, error : NSError?) -> ())
    {
        if httpmethod == HTTPMethod.POST
        {
        self.POST(urlString, parameters: parameters, success: { (_, result) -> Void in
            guard let dict = result as? [String : AnyObject] else
            {
                //执行失败的回调
                let myError = NSError(domain: errorDomain, code: -100, userInfo: [NSLocalizedDescriptionKey : "数据加载失败"])
                
                finish(dict: nil, error: myError)
                
                return
            }
              
                finish(dict: dict, error: nil)


            }) { (_, error) -> Void in
                //执行失败的回调
                finish(dict: nil, error: error)
                print(error)
        }
        }
        else
        {
            self.GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
                    guard let dict = result as? [String : AnyObject] else
                    {
                        //执行失败的回调
                        let myError = NSError(domain: errorDomain, code: -100, userInfo: [NSLocalizedDescriptionKey : "数据加载失败"])
                        
                        finish(dict: nil, error: myError)
                        
                        return
                    }
                    
                    finish(dict: dict, error: nil)
                    

                }, failure: { (_, error) -> Void in
                    //执行失败的回调
                    finish(dict: nil, error: error)
                    print(error)

            })
        }
    }
    
    //上传图片的方法
    func uploadImage(parameters : [String : String], imageData : NSData,urlString : String, finish:(dict : [String : AnyObject]?, error : NSError?) -> ()) {
       self.POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
        //formData 二进制数据, 其中name是服务器需要的字段,fileName随便写,mimeType是数据对应类型的字段(百度)
        formData.appendPartWithFileData(imageData, name: "pic", fileName: "whatever", mimeType: "image/jpeg")
        }, progress: { (p) -> Void in
            print(p)
        }, success: { (_, result) -> Void in
            guard let dict = result as? [String : AnyObject] else
            {
                //执行失败的回调
                let myError = NSError(domain: errorDomain, code: -100, userInfo: [NSLocalizedDescriptionKey : "数据加载失败"])
                
                finish(dict: nil, error: myError)
                
                return
            }
            
            finish(dict: dict, error: nil)
        }) { (_, error) -> Void in
            //执行失败的回调
            finish(dict: nil, error: error)
            print(error)

        }
    }
}